package com.sanerzone.smscenter.modules.sms.web;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.smscenter.modules.sms.entity.SmsPhoneBlacklistChange;
import com.sanerzone.smscenter.modules.sms.service.SmsPhoneBlacklistChangeService;
import com.sanerzone.smscenter.modules.sms.service.SmsPhoneBlacklistService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/4/11.
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsPhoneBlacklistChange")
public class SmsPhoneBlacklistChangeController extends BaseController{

    @Autowired
    private SmsPhoneBlacklistChangeService smsPhoneBlacklistChangeService;

    @Autowired
    private SmsPhoneBlacklistService smsPhoneBlacklistService;
    @ModelAttribute
    public SmsPhoneBlacklistChange get(@RequestParam(required=false) String id) {
        SmsPhoneBlacklistChange entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = smsPhoneBlacklistChangeService.get(id);
        }
        if (entity == null){
            entity = new SmsPhoneBlacklistChange();
        }
        return entity;
    }

    @RequiresPermissions("sms:smsPhoneBlacklistChange:view")
    @RequestMapping(value = "list")
    public String list(SmsPhoneBlacklistChange smsPhoneBlacklistChange, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SmsPhoneBlacklistChange> page = smsPhoneBlacklistChangeService.findPage(new Page<SmsPhoneBlacklistChange>(request, response), smsPhoneBlacklistChange);
        model.addAttribute("page", page);
        return "modules/sms/smsPhoneBlacklistChangeList";
    }

    @RequiresPermissions("sms:smsPhoneBlacklistChange:view")
    @RequestMapping(value = "form")
    public String form(SmsPhoneBlacklistChange smsPhoneBlacklistChange, Model model) {
        model.addAttribute("smsPhoneBlacklistChange", smsPhoneBlacklistChange);
        return "modules/sms/smsPhoneBlacklistChangeForm";
    }

    @RequiresPermissions("sms:smsPhoneBlacklistChange:edit")
    @RequestMapping(value = "update")
    public String update(SmsPhoneBlacklistChange smsPhoneBlacklistChange, RedirectAttributes redirectAttributes,Model model, HttpServletRequest request, HttpServletResponse response) {
        smsPhoneBlacklistChangeService.update(smsPhoneBlacklistChange);
        smsPhoneBlacklistService.updateByChangeId(smsPhoneBlacklistChange.getId());
        addMessage(redirectAttributes, "更改黑名单变更记录成功");
        Page<SmsPhoneBlacklistChange> page = smsPhoneBlacklistChangeService.findPage(new Page<SmsPhoneBlacklistChange>(request, response), smsPhoneBlacklistChange);
        model.addAttribute("page", page);
        return "modules/sms/smsPhoneBlacklistChangeList";
    }

    @RequiresPermissions("sms:smsPhoneBlacklistChange:edit")
    @RequestMapping(value = "delete")
    public String delete(SmsPhoneBlacklistChange smsPhoneBlacklistChange, RedirectAttributes redirectAttributes,Model model, HttpServletRequest request, HttpServletResponse response) {
        smsPhoneBlacklistChangeService.delete(smsPhoneBlacklistChange);
        smsPhoneBlacklistService.deleteByChangeId(smsPhoneBlacklistChange.getId());
        addMessage(redirectAttributes, "删除黑名单变更记录成功");
        Page<SmsPhoneBlacklistChange> page = smsPhoneBlacklistChangeService.findPage(new Page<SmsPhoneBlacklistChange>(request, response), smsPhoneBlacklistChange);
        model.addAttribute("page", page);
        return "modules/sms/smsPhoneBlacklistChangeList";
    }

}
